package cn.iocoder.yudao.module.system.controller.app.jd;

import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.jd.*;
import cn.iocoder.yudao.module.system.dal.dataobject.jd.RechargeDO;
import cn.iocoder.yudao.module.system.service.jd.RechargeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 京东提交生产")
@RestController
@RequestMapping("/jd/recharge")
@Validated
public class RechargeController {

    @Resource
    private RechargeService rechargeService;

    @PostMapping("/create")
    @Operation(summary = "创建京东提交生产")
//    @PreAuthorize("@ss.hasPermission('jd:recharge:create')")
    public CommonResult<Integer> createRecharge(@Valid @RequestBody RechargeSaveReqVO createReqVO) {
        RechargeDO recharge = rechargeService.getRecharge(createReqVO.getJdOrderNo());
        Integer id = rechargeService.createRecharge(createReqVO);
        if((recharge!=null&&createReqVO.getJdOrderNo().equals(recharge.getJdOrderNo()))
                || id ==-1){
            return error(GlobalErrorCodeConstants.UNIQUEINDEX.getCode(),GlobalErrorCodeConstants.UNIQUEINDEX.getMsg());
        }
        return success(id);
    }

    @PutMapping("/update")
    @Operation(summary = "更新京东提交生产")
    @PreAuthorize("@ss.hasPermission('jd:recharge:update')")
    public CommonResult<Boolean> updateRecharge(@Valid @RequestBody RechargeSaveReqVO updateReqVO) {
        rechargeService.updateRecharge(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除京东提交生产")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jd:recharge:delete')")
    public CommonResult<Boolean> deleteRecharge(@RequestParam("id") Integer id) {
        rechargeService.deleteRecharge(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得京东提交生产")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jd:recharge:query')")
    public CommonResult<RechargeRespVO> getRecharge(@RequestParam("id") Integer id) {
        RechargeDO recharge = rechargeService.getRecharge(id);
        return success(BeanUtils.toBean(recharge, RechargeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得京东提交生产分页")
    @PreAuthorize("@ss.hasPermission('jd:recharge:query')")
    public CommonResult<PageResult<RechargeRespVO>> getRechargePage(@Valid RechargePageReqVO pageReqVO) {
        PageResult<RechargeDO> pageResult = rechargeService.getRechargePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RechargeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出京东提交生产 Excel")
    @PreAuthorize("@ss.hasPermission('jd:recharge:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRechargeExcel(@Valid RechargePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RechargeDO> list = rechargeService.getRechargePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "京东提交生产.xls", "数据", RechargeRespVO.class,
                        BeanUtils.toBean(list, RechargeRespVO.class));
    }

    @PostMapping("/beginDisstill")
    @Operation(summary = "提交充值&提取卡密接口调用示例（京东调代理商）")
//    @PreAuthorize("@ss.hasPermission('system:order:query')")
    public CommonResult<SubmitRechargeResVO> beginDisstill(@Valid @RequestBody SubmitRechargeReqVO reqVO) {
        SubmitRechargeResVO resVo=rechargeService.beginDisstill(reqVO);
        return success(BeanUtils.toBean(resVo, SubmitRechargeResVO.class));
    }

    @PostMapping("/findDistill")
    @Operation(summary = "生产反查接口调用示例（京东调代理商）")
//    @PreAuthorize("@ss.hasPermission('system:order:query')")
    public CommonResult<SubmitRechargeResVO> findDistill(@Valid @RequestBody ProductionCheckReqVO reqVO) {
        SubmitRechargeResVO resVo=rechargeService.findDistill(reqVO);
        return success(BeanUtils.toBean(resVo, SubmitRechargeResVO.class));
    }

    @PostMapping("produceResult")
    @Operation(summary = "回调接口调用示例（代理商调京东）")
//    @PreAuthorize("@ss.hasPermission('system:order:query')")
    public HashMap<String,String> produceResult(@Valid @RequestBody ProduceResultReqVO reqVO) {
        return rechargeService.produceResult(10002,1);
    }

}